/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006-2009 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyVetoException;
import java.io.File;
import java.io.IOException;
import java.util.Map;

import javax.swing.JCheckBoxMenuItem;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

import org.apache.log4j.Logger;
import org.bushe.swing.event.EventBus;

import who.tmap.bo.ThematicMapLayer;
import who.tmap.common.TMapException;
import who.tmap.ui.event.ThematicMapLayerUpdated;
import who.tmap.ui.util.ArgumentsHelper;
import who.tmap.ui.util.ConfigFileHelper;
import who.tmap.ui.util.FileChooserFilter;
import who.tmap.ui.util.FramePreferences;
import who.tmap.ui.util.MiscUIHelper;
import who.tmap.ui.util.MultiLayerThematicMapUIHelper;
import who.tmap.ui.util.ThematicMapUIHelper;
import who.tmap.util.MiscHelper;

/**
 * This is the main window of TMap Designer
 * 
 * @author stl
 *
 */
public class JFrameMain extends JFrame {

    private static Logger logger = Logger.getLogger(JFrameMain.class);  
    
    private static final String TITLE ="TMap Designer Layer";
    
	private MiscHelper miscHelper =new MiscHelper();    
    
    private JFrameMain thisFrame =this;
    private JFrameMultiLayerThematicMap jFrameMultiLayerThematicMap =null;
    private ThematicMapLayer thematicMapLayer =null;
    
	private JDesktopPane jContentPane = null;

	private JMenuBar jMenuBar = null;
	private JMenu jMenuFile = null;
	private JMenuItem jMenuItemFileClose =null;	
	private JMenuItem jMenuItemFileCloseWithoutSaving = null;
	private JMenu jMenuEdit = null;
	private JMenuItem jMenuItemEditUndo = null;
	private JMenuItem jMenuItemEditRedo = null;
	private JMenuItem jMenuItemEditCut = null;
	private JMenuItem jMenuItemEditCopy = null;
	private JMenuItem jMenuItemEditPaste = null;
	private JMenu jMenuWindow = null;
	private JMenu jMenuHelp = null;
	private JMenuItem jMenuItemHelpAbout = null;
	private JMenuItem jMenuItemHelpPreferences = null;
	
	public JFrameMain jFrameMain =null;
	public JFrameThematicMap jFrameThematicMap =null;
	
	private JMenuItem jMenuItemFileNewWizard = null;
	
	private JMenuItem jMenuItemWindowTileVertically = null;
	private JMenuItem jMenuItemWindowTileHorizontally = null;

	private JMenuItem jMenuItemHelpHelp = null;
	private JMenuItem jMenuItemHelpLicense = null;
	private JMenuItem jMenuItemFileNewThematicMap = null;
	private JMenuItem jMenuItemFileOpenThematicMap = null;
	private JMenuItem jMenuItemFileSaveThematicMap = null;
	private JMenuItem jMenuItemFileSaveThematicMapAs = null;
	private JMenuItem jMenuItemEditClipboardCopy = null;
	private JMenuItem jMenuItemFilePrint = null;

	private JMenuItem jMenuItemThematicMapFullScreen = null;
	
	private MiscUIHelper miscUIHelper =new MiscUIHelper();

	private JCheckBoxMenuItem jMenuItemEditBaseMap = null;
	private JCheckBoxMenuItem jMenuItemEditData = null;
	private JMenuItem jMenuItemEditDataStructure = null;
	private JMenuItem jMenuItemEditNonMatchingInformation = null;
	private JCheckBoxMenuItem jMenuItemEditAnnotations = null;
	private JMenuItem jMenuItemEditMultiLayerThematicMap = null;

	/**
	 * This is the default constructor
	 */
	public JFrameMain() {
		super();
		
		jFrameMain =this;
		
		initialize();
		
		processArguments();
	}
	
	private void processArguments() {
		ArgumentsHelper argumentsHelper =ArgumentsHelper.getInstance();
		
		String name =argumentsHelper.getValue( ArgumentsHelper.NAME );
		if ( name != null ) {
			logger.debug( ArgumentsHelper.NAME + "=" + name );
			
			if ( !new File( name ).exists() ) {
				
				System.out.println( "Cannot find file " + name );
				dispose();
				
			} else if ( name.toLowerCase().endsWith( FileChooserFilter.MULTI_LAYER_THEMATIC_MAP_EXT ) ) {
				//setVisible( true );
				jFrameMultiLayerThematicMap =new JFrameMultiLayerThematicMap();
				//jFrameMultiLayerThematicMap.setVisible( true );
				
				new MultiLayerThematicMapUIHelper().openMultiLayerThematicMapFromArgument( name, jFrameMultiLayerThematicMap );
				
				if ( argumentsHelper.contains( ArgumentsHelper.PRINT ) ) {
					jFrameMultiLayerThematicMap.printThematicMap();
				} else if ( name != null ){
					jFrameMultiLayerThematicMap.displayFullScreen();
				}
				
				if ( argumentsHelper.contains( ArgumentsHelper.QUIET ) ) {
					jFrameMultiLayerThematicMap.dispose();
					dispose();
				} else {
					jFrameMultiLayerThematicMap.setVisible( true );
				}
				
			} else {
				new ThematicMapUIHelper( jFrameThematicMap ).openThematicMapUsingArgument( name );
				
				if ( argumentsHelper.contains( ArgumentsHelper.RELOAD ) ) {
					jFrameThematicMap.reloadFromDataSource( false );
				}
				
				if ( argumentsHelper.contains( ArgumentsHelper.PRINT ) ) {
					jFrameThematicMap.printThematicMap();
				} else if ( name != null ){
					jFrameThematicMap.displayThematicMapFullScreen();
				}
				
				if ( argumentsHelper.contains( ArgumentsHelper.QUIET ) ) {
					dispose();
				}		
			}
		}
		
	}
	
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(900, 600);
		this.setContentPane(getJContentPane());
		this.setLocationRelativeTo( null );
		this.setTitle( TITLE );
		
		setContentPane( getContentPane() );

		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		
		this.addWindowListener(new WindowAdapter() {   
			public void windowOpened(WindowEvent e) {
		        SwingUtilities.invokeLater( new Runnable() {
		            public void run() {
		            	arrangeAsSavedInThePreferences();
		            }
		        });
			}
			
			public void windowClosing(WindowEvent e) {
				closeWindow();
			}
		});	
		
		addComponentListener(new ComponentAdapter() {
			  public void componentResized(ComponentEvent e) {
			    logger.info( "JFrameMain.componentResized" );
	    
		        SwingUtilities.invokeLater( new Runnable() {
		            public void run() {
		            	jFrameThematicMap.rearrangeWindowsAfterResize();
		            }
		        });
			    
			  }
	  
		});
		
		setIconImage( miscHelper.getImageIcon( "ic_tmapdesigner.gif" ).getImage() );
		
		newThematicMap();
		
	}
	
	private void closeWindow() {
		try {
			new ConfigFileHelper().save( jFrameMain );
		} catch (IOException e1) {
			logger.error( e1 );
		}	
		
		applyLayerUpdate();
		
		dispose();
	}
	
	private void dimFrame( JInternalFrame jInternalFrame, FramePreferences prefs ) throws PropertyVetoException {
		jInternalFrame.setBounds( prefs.getRect() );
		
		if ( prefs.isIcon() ) {
			jInternalFrame.setIcon( true );
		} else if ( prefs.isMaximum() ) {
			jInternalFrame.setMaximum( true );
		}
	}
	
	private void arrangeAsSavedInThePreferences() {
		try {
			Map<Integer, FramePreferences> prefs =new ConfigFileHelper().load();
			
			if ( prefs.size() > 0 ) {
						
				FramePreferences mainPrefs =prefs.get( -1 );
				
				// Main window
				setBounds( mainPrefs.getRect() );
				
				if ( mainPrefs.getSplitPanePosition() != -1 ) {
					jFrameThematicMap.getJSplitPane()
									 .setDividerLocation( mainPrefs.getSplitPanePosition() );
				}
				
				for( int ct =prefs.size() -2; ct >= 0; ct-- ) {
					FramePreferences pref =prefs.get( ct );
					
					if ( ConfigFileHelper.MAP.equals( pref.getName() ) ) {
						dimFrame( jFrameThematicMap.jFrameMap, pref );
					} else if ( ConfigFileHelper.ATTRS.equals( pref.getName() ) ) {
						dimFrame( jFrameThematicMap.jFrameData, pref );
					} else if ( ConfigFileHelper.CD.equals( pref.getName() ) ) {
						dimFrame( jFrameThematicMap.jFrameCartographicDisplay, pref );
					} else if ( ConfigFileHelper.ANNOTATIONS.equals( pref.getName() ) ) {
						dimFrame( jFrameThematicMap.jFrameAnnotations, pref );
					}
				}

            	jFrameThematicMap.setAutoResize( true );
            	jFrameThematicMap.revalidate();
				
			// There are no preferences file yet
			} else {

				jFrameThematicMap.setAutoResize( true );
				jFrameThematicMap.initPreviousSize();
				jFrameThematicMap.tileVertically();

				jFrameThematicMap.rearrangeWindowsAfterResize();
            	jFrameThematicMap.revalidate();	 

			}
			
		} catch (Exception e) {
			logger.error( e );
		}
	}	
	
	/**
	 * Close the help window
	 *
	 */
	private void closeHelp() {
		miscUIHelper.closeHelp();
	}
		
	public void newThematicMap() {
		jFrameThematicMap = new JFrameThematicMap();
		jFrameThematicMap.setJFrameMain( this );
		jFrameThematicMap.setVisible(true); 

		jContentPane.add(jFrameThematicMap);
		try {
			jFrameThematicMap.setSelected(true);

			try {
				jFrameThematicMap.setMaximum( true );
			} catch (PropertyVetoException e) {
				logger.error( e );
			}		
			
			//frame.arrangeInternalFrames();
		} catch (java.beans.PropertyVetoException e2) {
			logger.error(e2);
		}	
	}
	
	/**
	 * This method initializes jContentPane
	 * 
	 * @return JPanel
	 */
	private JDesktopPane getJContentPane() {
		if ( jContentPane == null ) {
			jContentPane = new JDesktopPane();
			setJMenuBar(getTheMenuBar());
		}
		return jContentPane;
	}
	/**
	 * This method initializes jJMenuBar
	 * 
	 * @return JMenuBar
	 */
	private JMenuBar getTheMenuBar() {
		if ( jMenuBar == null ) {
			jMenuBar = new JMenuBar();
			jMenuBar.add(getJMenuFile());
			jMenuBar.add(getJMenuEdit());
			jMenuBar.add(getJMenuWindow());
			jMenuBar.add(getJMenuHelp());
		}
		return jMenuBar;
	}
	/**
	 * This method initializes jMenuFile
	 * 
	 * @return JMenu
	 */
	private JMenu getJMenuFile() {
		if ( jMenuFile == null ) {
			jMenuFile = new JMenu();
			jMenuFile.add(getJMenuItemFileNewThematicMap());
			jMenuFile.add(getJMenuItemFileOpenThematicMap());
			jMenuFile.add(getJMenuItemFileSaveThematicMap());
			jMenuFile.add(getJMenuItemFileSaveThematicMapAs());			
			jMenuFile.addSeparator();
			
			jMenuFile.add(getJMenuItemFileNewWizard());
			jMenuFile.add(getJMenuItemThematicMapFullScreen());			
			
			jMenuFile.addSeparator();			
			jMenuFile.add(getJMenuItemFilePrint());	
			
			jMenuFile.addSeparator();	
			jMenuFile.add(getJMenuItemFileClose());
			// STL 2009.01.06
			// May be in another version
			//jMenuFile.add(getJMenuItemFileCloseWithoutSaving());
			jMenuFile.setText("File");
			jMenuFile.setMnemonic('F');
		}
		return jMenuFile;
	}

/*	private JMenuItem getJMenuItemFileCloseWithoutSaving() {
		if(jMenuItemFileCloseWithoutSaving == null) {
			jMenuItemFileCloseWithoutSaving = new JMenuItem();
			jMenuItemFileCloseWithoutSaving.setText("Close Without Saving");
			jMenuItemFileCloseWithoutSaving.setMnemonic( 'W' );
			jMenuItemFileCloseWithoutSaving.addActionListener(new ActionListener() { 
				public void actionPerformed(ActionEvent e) {					
					//dispose();
					//quitApplication();
					closeWindow();
				}
			});
		}
		return jMenuItemFileCloseWithoutSaving;
	}*/
	
	private void applyLayerUpdate() {
		if ( thisFrame.jFrameThematicMap.jFrameMap.getMappingToolKit().getThematicMap() != null
				&& thisFrame.jFrameThematicMap.jFrameMap.getMappingToolKit().getThematicMap().isDirty() ) {
			thisFrame.jFrameThematicMap.jFrameMap.getMappingToolKit().unselectShapes();
			
			thematicMapLayer.setThematicMap( thisFrame.jFrameThematicMap.getThematicMap() );
			
			if ( thematicMapLayer.getThematicMap().getBaseMap() == null ) {
				try {
					thisFrame.jFrameThematicMap.getThematicMap().setBaseMap( 
							thisFrame.jFrameThematicMap.jFrameMap.getMappingToolKit().retrieveBaseMap() );
					
					EventBus.publish( new ThematicMapLayerUpdated() );
				} catch (TMapException e1) {
					logger.error( e1 );
				}
			}
			
		}
	}
	
	private JMenuItem getJMenuItemFileClose() {
		if ( jMenuItemFileClose == null ) {
			jMenuItemFileClose = new JMenuItem();
			jMenuItemFileClose.setText("Close");
			jMenuItemFileClose.setMnemonic( 'C' );
			jMenuItemFileClose.addActionListener(new ActionListener() { 
				public void actionPerformed(ActionEvent e) {

					
					closeWindow();
				}
			});
		}
		return jMenuItemFileClose;
	}	
	
	/**
	 * This method initializes jMenuEdit
	 * 
	 * @return JMenu
	 */
	private JMenu getJMenuEdit() {
		if ( jMenuEdit == null ) {
			jMenuEdit = new JMenu();
			jMenuEdit.setText("Edit");
			jMenuEdit.setMnemonic( 'E' );
			jMenuEdit.add(getJMenuItemEditBaseMap());
			jMenuEdit.add(getJMenuItemEditData());
			jMenuEdit.add(getJMenuItemEditDataStructure());
			jMenuEdit.add(getJMenuItemEditNonMatchingInformation());
			jMenuEdit.add(getJMenuItemEditAnnotations());
			jMenuEdit.add( getJMenuItemEditMultiLayerThematicMap() );
			jMenuEdit.addSeparator();
			jMenuEdit.add(getJMenuItemEditClipboardCopy());
		}
		return jMenuEdit;
	}
	
	/**
	 * This method initializes jMenuItemEditUndo
	 * 
	 * @return JMenuItem
	 */
	private JMenuItem getJMenuItemEditUndo() {
		if(jMenuItemEditUndo == null) {
			jMenuItemEditUndo = new JMenuItem();
			jMenuItemEditUndo.setText("Undo");
		}
		return jMenuItemEditUndo;
	}
	/**
	 * This method initializes jMenuItemEditRedo
	 * 
	 * @return JMenuItem
	 */
	private JMenuItem getJMenuItemEditRedo() {
		if(jMenuItemEditRedo == null) {
			jMenuItemEditRedo = new JMenuItem();
			jMenuItemEditRedo.setText("Redo");
	
		}
		return jMenuItemEditRedo;
	}
	
	/**
	 * This method initializes jMenuItemEditCut
	 * 
	 * @return JMenuItem
	 */
	private JMenuItem getJMenuItemEditCut() {
		if(jMenuItemEditCut == null) {
			jMenuItemEditCut = new JMenuItem();
			jMenuItemEditCut.setText("Cut");
		}
		return jMenuItemEditCut;
	}
	
	/**
	 * This method initializes jMenuItemEditCopy
	 * 
	 * @return JMenuItem
	 */
	private JMenuItem getJMenuItemEditCopy() {
		if(jMenuItemEditCopy == null) {
			jMenuItemEditCopy = new JMenuItem();
			jMenuItemEditCopy.setText("Copy");
			jMenuItemEditCopy.addActionListener(new ActionListener() { 
				public void actionPerformed(ActionEvent e) {    
				    JFrameThematicMap tmf =(JFrameThematicMap)getJContentPane().getSelectedFrame();
				    tmf.clipboardCopy();
				}
			});
		}
		return jMenuItemEditCopy;
	}
	
	/**
	 * This method initializes jMenuItemEditPaste
	 * 
	 * @return JMenuItem
	 */
	private JMenuItem getJMenuItemEditPaste() {
		if(jMenuItemEditPaste == null) {
			jMenuItemEditPaste = new JMenuItem();
			jMenuItemEditPaste.setText("Paste");
		}
		return jMenuItemEditPaste;
	}
	
	/**
	 * This method initializes jMenuWindow
	 * 
	 * @return JMenu
	 */
	private JMenu getJMenuWindow() {
		if(jMenuWindow == null) {
			jMenuWindow = new JMenu();
			jMenuWindow.setText("Window");
			jMenuWindow.setMnemonic( 'W' );
			jMenuWindow.add(getJMenuItemWindowTileHorizontally());			
			jMenuWindow.add(getJMenuItemWindowTileVertically());
		}
		return jMenuWindow;
	}
	
	/**
	 * This method initializes jMenuHelp
	 * 
	 * @return JMenu
	 */
	private JMenu getJMenuHelp() {
		if(jMenuHelp == null) {
			jMenuHelp = new JMenu();
			jMenuHelp.setMnemonic( 'H' );
			jMenuHelp.setText("Help");
			
			jMenuHelp.add(getJMenuItemHelpPreferences());

			jMenuHelp.add(getJMenuItemHelpHelp());		
			jMenuHelp.add(getJMenuItemHelpLicense());
			jMenuHelp.addSeparator();				
			jMenuHelp.add(getJMenuItemHelpAbout());

		}
		return jMenuHelp;
	}
	
	/**
	 * This method initializes jMenuItemHelpAbout
	 * 
	 * @return JMenuItem
	 */
	private JMenuItem getJMenuItemHelpAbout() {
		if(jMenuItemHelpAbout == null) {
			jMenuItemHelpAbout = new JMenuItem();
			jMenuItemHelpAbout.setMnemonic( 'A' );
			jMenuItemHelpAbout.setText("About TMap Designer");
			jMenuItemHelpAbout.addActionListener(new ActionListener() { 
				public void actionPerformed(ActionEvent e) {    
					String libVersion =thisFrame.jFrameThematicMap.jFrameMap.getMappingToolKit().getVersion();
					JDialogAbout aboutBox =new JDialogAbout( JOptionPane.getFrameForComponent(thisFrame), true, libVersion, new MiscUIHelper().getBuildDate() );
					
					aboutBox.setVisible( true );					
				}
			});
		}
		return jMenuItemHelpAbout;
	}
	
	/**
	 * This method initializes jMenuItemHelpPreferences
	 * 
	 * @return JMenuItem
	 */
	private JMenuItem getJMenuItemHelpPreferences() {
		
		if(jMenuItemHelpPreferences == null) {
			jMenuItemHelpPreferences = new JMenuItem();
			jMenuItemHelpPreferences.setText("Preferences");
			jMenuItemHelpPreferences.setVisible(false);
			jMenuItemHelpPreferences.addActionListener(new ActionListener() { 
				public void actionPerformed(ActionEvent e) {
					JDialogPreferences jDialogPreferences =new JDialogPreferences( jFrameMain, "Preferences", true );
					jDialogPreferences.setVisible( true );
				}
			});
		}
		return jMenuItemHelpPreferences;
	}
	
	/**
	 * This method initializes jMenuItemFileNewWizard
	 * 
	 * @return JMenuItem
	 */
	private JMenuItem getJMenuItemFileNewWizard() {
		if(jMenuItemFileNewWizard == null) {
			jMenuItemFileNewWizard = new JMenuItem();
			jMenuItemFileNewWizard.setText("New Thematic Map Wizard...");
			jMenuItemFileNewWizard.setMnemonic( 'W' );
			jMenuItemFileNewWizard.addActionListener(new ActionListener() { 
				public void actionPerformed(ActionEvent e) {    
					thisFrame.jFrameThematicMap.startWizardMode();
				}
				
			});
		}
		return jMenuItemFileNewWizard;
	}
	

	/**
	 * This method initializes jMenuItemWindowVerticalArrangement	
	 * 	
	 * @return JMenuItem	
	 */
	private JMenuItem getJMenuItemWindowTileVertically() {
		if (jMenuItemWindowTileVertically == null) {
			jMenuItemWindowTileVertically = new JMenuItem();
			jMenuItemWindowTileVertically.setText("Tile Vertically");
			jMenuItemWindowTileVertically.setMnemonic( 'V' );
			jMenuItemWindowTileVertically
					.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent e) {
							jFrameThematicMap.tileVertically();
						}
					});
		}
		return jMenuItemWindowTileVertically;
	}
	/**
	 * This method initializes jMenuItemWindowHorizontalArrangement	
	 * 	
	 * @return JMenuItem	
	 */
	private JMenuItem getJMenuItemWindowTileHorizontally() {
		if (jMenuItemWindowTileHorizontally == null) {
			jMenuItemWindowTileHorizontally = new JMenuItem();
			jMenuItemWindowTileHorizontally.setText("Tile Horizontally");
			jMenuItemWindowTileHorizontally.setMnemonic( 'H' );			
			jMenuItemWindowTileHorizontally
					.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent e) {
							jFrameThematicMap.tileHorizontally();						
						}
					});
		}
		return jMenuItemWindowTileHorizontally;
	}
	/**
	 * This method initializes jMenuItemHelpHelp	
	 * 	
	 * @return JMenuItem	
	 */
	private JMenuItem getJMenuItemHelpHelp() {
		if (jMenuItemHelpHelp == null) {
			jMenuItemHelpHelp = new JMenuItem();
			jMenuItemHelpHelp.setText("Help");
			jMenuItemHelpHelp.setMnemonic( 'H' );
			jMenuItemHelpHelp.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					miscUIHelper.showHelp();
				}
			});
		}
		return jMenuItemHelpHelp;
	}
	/**
	 * This method initializes jMenuItemHelpLicense	
	 * 	
	 * @return JMenuItem	
	 */
	private JMenuItem getJMenuItemHelpLicense() {
		if (jMenuItemHelpLicense == null) {
			jMenuItemHelpLicense = new JMenuItem();
			jMenuItemHelpLicense.setText("License");
			jMenuItemHelpLicense.setMnemonic( 'L' );
			jMenuItemHelpLicense.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					JDialogLicense jDialogLicense =new JDialogLicense( JOptionPane.getFrameForComponent(thisFrame), true );
					
					jDialogLicense.setVisible( true );
				}
			});
		}
		return jMenuItemHelpLicense;
	}
	/**
	 * This method initializes jMenuItemFileNewThematicMap	
	 * 	
	 * @return JMenuItem	
	 */
	private JMenuItem getJMenuItemFileNewThematicMap() {
		if (jMenuItemFileNewThematicMap == null) {
			jMenuItemFileNewThematicMap = new JMenuItem();
			jMenuItemFileNewThematicMap.setText("New");
			jMenuItemFileNewThematicMap.setMnemonic( 'N' );
			jMenuItemFileNewThematicMap
					.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent e) {
							jFrameThematicMap.newThematicMap();
						}
					});
		}
		return jMenuItemFileNewThematicMap;
	}
	
	/**
	 * This method initializes jMenuItemFileOpenThematicMap	
	 * 	
	 * @return JMenuItem	
	 */
	private JMenuItem getJMenuItemFileOpenThematicMap() {
		if (jMenuItemFileOpenThematicMap == null) {
			jMenuItemFileOpenThematicMap = new JMenuItem();
			jMenuItemFileOpenThematicMap.setText("Open...");
			jMenuItemFileOpenThematicMap.setMnemonic( 'O' );
			jMenuItemFileOpenThematicMap
					.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent e) {
							jFrameThematicMap.openThematicMap();
						}
					});
		}
		return jMenuItemFileOpenThematicMap;
	}
	/**
	 * This method initializes jMenuItemSaveThematicMap	
	 * 	
	 * @return JMenuItem	
	 */
	private JMenuItem getJMenuItemFileSaveThematicMap() {
		if (jMenuItemFileSaveThematicMap == null) {
			jMenuItemFileSaveThematicMap = new JMenuItem();
			jMenuItemFileSaveThematicMap.setText("Save");
			jMenuItemFileSaveThematicMap.setMnemonic( 'S' );
			jMenuItemFileSaveThematicMap.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					jFrameThematicMap.saveThematicMap();
				}
			});
		}
		return jMenuItemFileSaveThematicMap;
	}
	/**
	 * This method initializes jMenuItemFileSaveThematicMapAs	
	 * 	
	 * @return JMenuItem	
	 */
	private JMenuItem getJMenuItemFileSaveThematicMapAs() {
		if (jMenuItemFileSaveThematicMapAs == null) {
			jMenuItemFileSaveThematicMapAs = new JMenuItem();
			jMenuItemFileSaveThematicMapAs.setText("Save As...");
			jMenuItemFileSaveThematicMapAs.setMnemonic( 'A' );
			jMenuItemFileSaveThematicMapAs
					.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent e) {
							jFrameThematicMap.saveThematicMapAs();
						}
					});
		}
		return jMenuItemFileSaveThematicMapAs;
	}
	/**
	 * This method initializes jMenuItemFileClipboardCopy	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getJMenuItemEditClipboardCopy() {
		if (jMenuItemEditClipboardCopy == null) {
			jMenuItemEditClipboardCopy = new JMenuItem();
			jMenuItemEditClipboardCopy.setText("Copy Thematic Map To Clipboard");
			jMenuItemEditClipboardCopy.setMnemonic( 'C' );
			jMenuItemEditClipboardCopy
					.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent e) {
							jFrameThematicMap.clipboardCopy();
						}
					});
		}
		return jMenuItemEditClipboardCopy;
	}
	/**
	 * This method initializes jMenuItemFilePrint	
	 * 	
	 * @return JMenuItem	
	 */
	private JMenuItem getJMenuItemFilePrint() {
		if (jMenuItemFilePrint == null) {
			jMenuItemFilePrint = new JMenuItem();
			jMenuItemFilePrint.setText("Print...");
			jMenuItemFilePrint.setMnemonic( 'P' );
			jMenuItemFilePrint.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					jFrameThematicMap.printThematicMap();
				}
			});
		}
		return jMenuItemFilePrint;
	}
	
	
	/**
	 * This method initializes jMenuItemThematicMapFullScreen	
	 * 	
	 * @return JMenuItem	
	 */
	private JMenuItem getJMenuItemThematicMapFullScreen() {
		if (jMenuItemThematicMapFullScreen == null) {
			jMenuItemThematicMapFullScreen = new JMenuItem();
			jMenuItemThematicMapFullScreen.setText("Full Screen...");
			jMenuItemThematicMapFullScreen.setMnemonic( 'F' );
			jMenuItemThematicMapFullScreen
					.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent e) {
							jFrameThematicMap.displayThematicMapFullScreen();
						}
					});
		}
		return jMenuItemThematicMapFullScreen;
	}

	/**
	 * This method initializes jMenuItemEditBaseMap	
	 * 	
	 * @return JMenuItem	
	 */
	public JMenuItem getJMenuItemEditBaseMap() {
		if (jMenuItemEditBaseMap == null) {
			jMenuItemEditBaseMap = new JCheckBoxMenuItem();
			jMenuItemEditBaseMap.setText("Base Map");
			jMenuItemEditBaseMap.setMnemonic('M');
			jMenuItemEditBaseMap.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					thisFrame.jFrameThematicMap.getJCheckBoxMenuItemBaseMapEdit().setSelected( jMenuItemEditBaseMap.isSelected() );
				}
			});
		}
		return jMenuItemEditBaseMap;
	}

	/**
	 * This method initializes jMenuItemEditData	
	 * 	
	 * @return JMenuItem	
	 */
	public JMenuItem getJMenuItemEditData() {
		if (jMenuItemEditData == null) {
			jMenuItemEditData = new JCheckBoxMenuItem();
			jMenuItemEditData.setText("Data");
			jMenuItemEditData.setMnemonic( 'D' );
			jMenuItemEditData.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					thisFrame.jFrameThematicMap.getJCheckBoxMenuItemDataEdit().setSelected( jMenuItemEditData.isSelected() );
				}
			});
		}
		return jMenuItemEditData;
	}

	/**
	 * This method initializes jMenuItemEditDataStructure	
	 * 	
	 * @return JMenuItem	
	 */
	private JMenuItem getJMenuItemEditDataStructure() {
		if (jMenuItemEditDataStructure == null) {
			jMenuItemEditDataStructure = new JMenuItem();
			jMenuItemEditDataStructure.setText("Data Structure...");
			jMenuItemEditDataStructure.setMnemonic( 'S' );
			jMenuItemEditDataStructure
					.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent e) {
							jFrameThematicMap.editDataStructure();	
						}
					});
		}
		return jMenuItemEditDataStructure;
	}

	/**
	 * This method initializes jMenuItemEditNonMatchingInformation	
	 * 	
	 * @return JMenuItem	
	 */
	private JMenuItem getJMenuItemEditNonMatchingInformation() {
		if (jMenuItemEditNonMatchingInformation == null) {
			jMenuItemEditNonMatchingInformation = new JMenuItem("Non Matching Information...");
			jMenuItemEditNonMatchingInformation.setMnemonic( 'N' );
			jMenuItemEditNonMatchingInformation
					.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent e) {
							jFrameThematicMap.editCartographicDisplayNonMatchingInfo();						
						}
					});
		}
		return jMenuItemEditNonMatchingInformation;
	}
	
	private JMenuItem getJMenuItemEditMultiLayerThematicMap() {
		if (jMenuItemEditMultiLayerThematicMap == null) {
			jMenuItemEditMultiLayerThematicMap = new JMenuItem("Multi-Layer Thematic Map...");
			jMenuItemEditMultiLayerThematicMap.setMnemonic( 'L' );
			jMenuItemEditMultiLayerThematicMap
					.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent e) {
							if ( jFrameMultiLayerThematicMap == null ) {
								jFrameMultiLayerThematicMap =new JFrameMultiLayerThematicMap();
							} 
							
							jFrameMultiLayerThematicMap.setVisible( true );
						}
					});
		}
		return jMenuItemEditMultiLayerThematicMap;
	}

	/**
	 * This method initializes jMenuItemEditAnnotations	
	 * 	
	 * @return JMenuItem	
	 */
	public JMenuItem getJMenuItemEditAnnotations() {
		if (jMenuItemEditAnnotations == null) {
			jMenuItemEditAnnotations = new JCheckBoxMenuItem("Annotations");
			jMenuItemEditAnnotations.setMnemonic( 'A' );
			jMenuItemEditAnnotations.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					thisFrame.jFrameThematicMap.getJCheckBoxMenuItemAnnotationsEdit().setSelected( jMenuItemEditAnnotations.isSelected() );
				}
			});
		}
		return jMenuItemEditAnnotations;
	}

	public void setThematicMapLayer(ThematicMapLayer thematicMapLayer) {
		this.thematicMapLayer = thematicMapLayer;
		
		this.setTitle( TITLE + " - " + thematicMapLayer.getName() );
		//thematicMapLayer.getFile().getName() );
	}
	
 }
